package com.kt.uokos.yimo.controller;


import com.kt.uokos.yimo.anno.RequiresPermissionsDesc;
import com.kt.uokos.yimo.anno.validator.Order;
import com.kt.uokos.yimo.anno.validator.Sort;
import com.kt.uokos.yimo.dao.Coupon;
import com.kt.uokos.yimo.dao.CouponUser;
import com.kt.uokos.yimo.service.CouponService;
import com.kt.uokos.yimo.service.CouponUserService;
import com.kt.uokos.yimo.util.ResponseUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/coupon")
public class CouponController {

    @Resource
    private CouponService couponService;

    @Resource
    private CouponUserService couponUserService;

    @RequiresPermissions("admin:coupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name, Short type, Short status,
                       @RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10")Integer limit,
                       @Sort@RequestParam(defaultValue = "add_time")String sort,
                       @Order@RequestParam(defaultValue = "desc")String order){

        List<Coupon> coupons = couponService.querySelective(name, type, status, page, limit, sort, order);
        return ResponseUtil.okList(coupons);
    }

    @RequiresPermissions("admin:coupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody Coupon coupon){
        if (coupon ==null){
            return ResponseUtil.errs();
        }
        couponService.add(coupon);
        return ResponseUtil.ok();
    }


    @RequiresPermissions("admin:coupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody Coupon coupon){
        couponService.delete(coupon.getId());
        return ResponseUtil.ok();
    }


    @RequiresPermissions("admin:coupon:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id){
        Coupon coupon = couponService.queryById(id);
        return ResponseUtil.ok(coupon);
    }

    @PostMapping("/update")
    public Object update(@RequestBody Coupon coupon){
        couponService.update(coupon);
        return ResponseUtil.ok();
    }

    @GetMapping("listuser")
    public Object listuser(Integer userId, Integer couponId, Short status,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit,
                           @Sort @RequestParam(defaultValue = "add_time") String sort,
                           @Order @RequestParam(defaultValue = "desc") String order){
        List<CouponUser> couponUsers = couponUserService.queryList(userId, couponId, status, page, limit, sort, order);
        return ResponseUtil.okList(couponUsers);
    }

}
